package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.util.ChZhRJUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CuteBabyAvatarView extends FrameLayout {

    public CuteBabyAvatarView(Context context) {
        super(context);
        init();
    }

    public CuteBabyAvatarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CuteBabyAvatarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private RoundedImageView avatarView;
    private View layout, bgLayout;

    private void init() {
        layout = LayoutInflater.from(getContext()).inflate(R.layout.cute_avatar_view, this);
        avatarView = (RoundedImageView) layout.findViewById(R.id.avatar_view);
        bgLayout = findViewById(R.id.avatar_bg);
    }

    public void setAvatar(String url) {
        url = ChZhRJUtils.getText(url, "");
        if(!ImageLoader.getInstance().getDiskCache().get(url).exists()) {
            ImageLoader.getInstance().displayImage(url, avatarView, Config.getOptionsHeadUnKnown_Exists());
        } else {
            ImageLoader.getInstance().displayImage(url, avatarView, Config.getOptionsHeadUnKnown());
        }
    }

    public void setSex(int sex) {
        float density = getContext().getResources().getDisplayMetrics().density;
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) avatarView.getLayoutParams();
        if(params == null) {
            params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        }
        layout.setPadding(0, 0, 0, 0);
        float scaleX = getLayoutParams().width / (187 * density / 2);
        float scaleY = getLayoutParams().height / (201 * density / 2);
        if(sex == 1) {
            bgLayout.setBackgroundResource(R.mipmap.baby_head_bg_1);
            layout.setPadding(0, (int) (2.5 * density * scaleX), 0, (int)(2.5 * density * scaleX));
            params.setMargins((int) (2 * density * scaleX), (int) (27.8 * density * scaleY), (int) (5.5 * density * scaleX), (int) (1.5 * density * scaleY));
            avatarView.setCornerRadius(0, 0, 5 * density * scaleX, 5 * density * scaleX);
        } else if(sex == 2) {
            bgLayout.setBackgroundResource(R.mipmap.baby_head_bg_2);
            params.setMargins((int) (2 * density * scaleX), (int) (31 * density * scaleY), (int) (5.5 * density * scaleX), (int) (1.5 * density * scaleY));
            avatarView.setCornerRadius(0, 0, 5 * density * scaleX, 5 * density * scaleX);
        } else {
            bgLayout.setBackgroundResource(R.mipmap.baby_head_bg_3);
            layout.setPadding(0, (int)(6 * density * scaleX), (int)(4 * density * scaleX), 0);
            params.setMargins((int) (2 * density * scaleX), (int) (24.6 * density * scaleY), (int) (1.5 * density * scaleY), (int) (1.5 * density * scaleY));
        }
        avatarView.setCornerRadius(0, 0, (int)(4.5 * density * scaleX), (int)(4.5 * density * scaleX));
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(avatarView.getDrawable() != null) {
            avatarView.getDrawable().setCallback(null);
        }
        if(bgLayout.getBackground() != null) {
            bgLayout.getBackground().setCallback(null);
        }
    }
}
